'''
Created on Jul 17, 2012

@author: byron
'''
import wx
import matplotlib
matplotlib.interactive(False)
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
from matplotlib.figure import Figure

class GraphPanel(wx.Window):
    def __init__(self, *args, **kwargs):
        wx.Window.__init__(self, *args, **kwargs)
        self.figure = Figure()
        self.canvas = FigureCanvasWxAgg(self, -1, self.figure)
        self.tmp_ax = self.figure.add_subplot(211)
        self.hum_ax = self.tmp_ax.twinx()

        
    def draw(self):
        data = self.getData()
        self.tmp_stats = self.Parent.stats.TemperatureStats()
        self.hum_stats = self.Parent.stats.HumidityStats()

        self.tmp_ax.clear()        
        self.tmp_ax.grid(True)
        self.tmp_ax.set_ylim(round(self.tmp_stats[2]-10), round(self.tmp_stats[2]+10))
        self.tmp_ax.plot(data['temp'], linewidth=2, color=(0, 0, 1), label="Temperature")
        
        self.hum_ax.clear()
        self.hum_ax.plot(data['hum'], linewidth=2, color=(1, 0, 0), label="Humidity")
        self.hum_ax.set_ylim(self.hum_stats[2]-20, self.hum_stats[2]+20)

        self.canvas.draw()
        
    def getData(self):
        stats = self.Parent.stats.GetLogs()
        stats.reverse()
        data = {'date' : [], 'temp' : [], 'hum'  : []}

        for i in stats:
            data['date'].append(i[0])
            data['temp'].append(i[1])
            data['hum'].append(i[2])
        
        return data